﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using System.ComponentModel;
using Jbe.TestSuite.Help.Interface;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Jbe.Composite.Extensions;
using System.Windows.Input;
using System.Windows;
using System.Reflection;
using Jbe.TestSuite.Help.Properties;

namespace Jbe.TestSuite.Help.Applications
{
    internal class HelpPresentationModel : PresentationModel<IHelpView>, IHeaderInfoProvider
    {
        private readonly IDynamicHelp helpService;
        private readonly DelegateCommand browseHomeCommand;
        private Uri helpSource;
        private Uri homeSource;


        public HelpPresentationModel(IHelpView view, IDynamicHelp helpService)
            : base(view)
        {
            this.browseHomeCommand = new DelegateCommand(BrowseHome, () => homeSource != null);
            this.helpService = helpService;
            HelpUriChangedEventManager.AddListener(helpService, this);
        }


        public string HeaderInfo
        {
            get { return Resources.HelpTitle; }
        }

        public ImageSource HeaderIcon
        {
            get { return new BitmapImage(ResourceService.GetPackUri("Resources/Help.ico")); }
        }

        public ICommand BrowseHomeCommand { get { return browseHomeCommand; } }

        public Uri HelpSource
        {
            get { return helpSource; }
            set
            {
                if (helpSource != value)
                {
                    helpSource = value;
                    if (homeSource == null) { homeSource = value; }
                    RaisePropertyChanged("HelpSource");
                }
            }
        }

        protected override void ReceiveWeakEvent(Type managerType, object sender, EventArgs e, ref bool handled)
        {
            base.ReceiveWeakEvent(managerType, sender, e, ref handled);

            if (managerType == typeof(HelpUriChangedEventManager) && sender == helpService)
            {
                HelpUriChanged(sender, e);
                handled = true;
            }
        }

        private void BrowseHome()
        {
            HelpSource = homeSource;
        }

        private void HelpUriChanged(object sender, EventArgs e)
        {
            HelpSource = helpService.HelpUri;
        }


        private sealed class HelpUriChangedEventManager : WeakEventManagerBase<HelpUriChangedEventManager, IDynamicHelp>
        {
            protected override void StartListening(IDynamicHelp source)
            {
                source.HelpUriChanged += HelpUriChanged;
            }

            protected override void StopListening(IDynamicHelp source)
            {
                source.HelpUriChanged -= HelpUriChanged;
            }

            private void HelpUriChanged(object sender, EventArgs e)
            {
                base.DeliverEvent(sender, e);
            }
        }
    }
}
